<?php
class Bound {
	
	/** 采购入库 */
	const TYPE_CK = 1;
	/** 工程移交 */
	const TYPE_YJ = 2;
	/** 退料入库 */
	const TYPE_TL = 3;
	/** 盘盈调入 */
	const TYPE_PY = 4;
	/** 领料出库 */
	const TYPE_LL = 5;
	/** 盘亏调出 */
	const TYPE_PK = 6;
	
	public static function add($data){
		return Db::get(__CLASS__)->insert($data)->exec() ? Db::get(__CLASS__)->insertId() : false;
	}
	
	public static function get($id){
		return Db::get('_Bound')->select()->where(array('id'=>$id))->getOne();
	}
	
	/**
	 * Rever::updateFlow的回调接口
	 * 更新Bound订单的审核状态(如果有必要)
	 */
	public static function updateFlow($id, $audit_flow, $type=null){
		if($type==Reviewer::CLS_QC && $audit_flow==Reviewer::AUDIT_PASS){
			//采购入库质检通过，影响库存
			if(!self::inboundQc($id)){
				return false;
			}
		}
		
		return Db::get(__CLASS__)->update(array('audit_flow'=>$audit_flow))
								->where(array('id'=>$id))->exec();
	}
	
	public static function update($bound_id, $data){
		return Db::get(__CLASS__)->update($data)->where(array('id'=>$bound_id))->exec()
			&& Db::get(__CLASS__)->affectedRows()>0 ? true: false;
	}
	
	/**
	 * 从indentcontroller移来的代码，因需求变化造成移动
	 */
	public static function inboundQc($boundid){
		$bound = Bound::get($boundid);
		if(!$bound){
			self::$_err = '取采购入库单失败';
			return false;
		}
		if(!Bound::update($boundid, array('effect'=>1))){
			self::$_err = '设置采购入库单状态失败';
			return false;
		}

		//入库单明细取自采购订单明细
		$indent_id = $bound['indent_id'];
		$indent_items = IndentItems::getByIndent($indent_id);

		$bound_items = BoundItems::getByBound($boundid);
		$total = 0.0;
		foreach($bound_items as $itemid=>$boundItem){

			$indentItem = $indent_items[$itemid];
			$amount = $boundItem['amount'];

			$total += $boundItem['total'];
			
			//更新材料库存价值
			if(!Ware::renewValues(array('amount'=>$amount,'price'=>$indentItem['price'],'ware_id'=>$itemid), 1, $errmsg)){
				self::$_err = "修改结存信息失败：{$indentItem['name']}：$errmsg";
				return false;
			}
			$ware = Ware::getValues($itemid);

			if(!BoundItems::update($boundid, $itemid, array('effect'=>1,
					'bl_amount'	=> $ware['amount'],
					'bl_price'	=> $ware['price'],
					'bl_total'	=> $ware['total'],
				))){
				self::$_err = '设置材料状态失败';
				return false;
			}

			//设置需求明细的回库数量，这个是算出来的，而且是按需求ID依次满足
			$list = Db::get('_DemandItems')->select('t0.demand_id,t0.amount,t0.amount-t0.amount_back as amount_diff')
					->join('_IndentItems')->on('t1.summary_id=t0.summary_id')
					->where("t1.indent_id={$indent_id} and t0.ware_id={$itemid} and t1.ware_id={$itemid}")
					->getAll();
			$num = $amount;
			$len = count($list);
			for($i=0;$i<$len && $num>0;$i++){
				$_amt = 0;
				if($num < $list[$i]['amount_diff']){
					$_amt = $num;
					$num = 0;
				}
				else{
					$_amt = $list[$i]['amount_diff'];
					$num -= $list[$i]['amount_diff'];
				}
				Db::get('DemandItems')->update("amount_back=amount_back+{$_amt}")
						->where(array('demand_id'=>$list[$i]['demand_id'], 'ware_id'=>$itemid))->exec();
			}

		}

		Db::get('Bound')->update(array('total'=>$total))->where(array('id'=>$boundid))->exec();
		Db::get('Indent')->update(array('back_flow'=>1))->where(array('id'=>$indent_id))->exec();

		return true;
	}
	static function getError(){
		return isset(self::$_err) ? self::$_err : '';
	}
	
}

?>
